
; Weapon Base Class
; combines weapon damage and graphics 


(load "damage.scm")
(load "graphics-object.scm")


(define weapon%
  (class damage%
    
    ; PHYSICS-OBJECT%
    (inherit-field position)
    ; PUBLIC VARIABLES
    (init-field [weapon-name  #f]
                [weapon-speed #i0]
                [clip-size    0]
                [fire-rate    #i0]
                [reload-time  #i0])
    
    ; PRIVATE VARIABLES
    (define graphics-weapon (new graphics-object% [graphics-name (string-char-replace weapon-name (lambda (char) (if (eq? #\space char) "-" (string char))))]))
    (define clip-current   clip-size)
    (define fire-current   #i0)
    (define reload-current #i0)
    (define ready-to-fire  #t)
    
    ; GRAPHICS-OBJECT%
    (delegate anim-degree graphics-weapon)
    
    ; GETTERS
    (define/public (get-weapon-speed) weapon-speed)
    (define/public (get-weapon-name)  weapon-name)
    
    
    ; Ready To Fire
    (define/public (ready-to-fire?)
      ready-to-fire)
    
    ; Fire
    (define/public (fire)
      (when ready-to-fire
        (set! clip-current (sub1 clip-current))
        (set! ready-to-fire #f)))
    
    ; Get Clip
    (define/public (get-clip)
      (fl/ (fixnum->flonum clip-current) (fixnum->flonum clip-size)))
    
    ; Get Reload
    (define/public (get-reload)
      (fl/ (fixnum->flonum reload-current) (fixnum->flonum reload-time)))
    
    ; Draw
    (define/override (draw dc)
      (send graphics-weapon draw dc (get-x position) (get-y position)))
    
    ; Weapon Load
    (define/public (weapon-load)
      (if (= clip-current 0)
          ; reload clip
          (begin
            (set! reload-current (fl+ reload-current *time-delta*))
            (when (fl>= reload-current reload-time)
              (set! reload-current #i0)
              (set! clip-current clip-size)
              (set! ready-to-fire #t)))
          ; fire rate
          (begin
            (unless ready-to-fire
              (set! fire-current (fl+ fire-current *time-delta*))
              (when (fl>= fire-current fire-rate)
                (set! fire-current #i0)
                (set! ready-to-fire #t))))))
          
    ; Constructor/Initialization
    (super-new [size (make-size graphics-weapon)])))


